#include "FrameworkTestapp.h"

#include "QtFwk/GLGraphicsView.h"

#include "OpenGL/OpenGL.h"
#include "TestScene.h"
#include "CAD/CadEngine.h"
#include "CAD/CadTool.h"

#include "Utils/MemCheck.h"


// ------------------------------------------------------------------------------------------------
FrameworkTestApp::FrameworkTestApp(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
	ui.setupUi(this);

	gui_createGLArea();
	gui_createToolbar();

	OpenGL::glDumpContextInfo();
}

// ------------------------------------------------------------------------------------------------
FrameworkTestApp::~FrameworkTestApp()
{}

// ------------------------------------------------------------------------------------------------
void FrameworkTestApp::gui_createGLArea()
{
	//QtFwk::GLGraphicsView::embedScene(new TestScene(0),this,ui.centralWidget->layout(),false);

	QtFwk::GLGraphicsView  *view  = new QtFwk::GLGraphicsView( this,false );
	_scene                        = new TestScene( view );
	view->setScene( _scene );

	QVBoxLayout *layout = new QVBoxLayout;
	layout->setContentsMargins(0,0,0,0);
	layout->addWidget( view );

	ui.frameGLArea->setLayout(layout);
}

// ------------------------------------------------------------------------------------------------
void FrameworkTestApp::gui_createToolbar()
{
	QToolBar *tb = ui.mainToolBar;
	QActionGroup *ag    = new QActionGroup( tb );

	// default tool (pan)
	QAction *pan = new QAction( QIcon(":/Icons/CAD_Pointer.png"), tr("Pan"), this);
	pan->setData( QVariant::fromValue<int>(-1) );
	pan->setCheckable(true);
	pan->setChecked(true);

	tb->addAction( pan  );
	tb->addSeparator();

	ag->addAction(pan);

	// add remaining tools from cad engine
	CadEngine::CadTools cadTools = _scene->cadEngine()->tools();

	for( CadEngine::CadTools::iterator it = cadTools.begin();
		it != cadTools.end();
		++it )
	{
		QAction *action = new QAction( QIcon(QString(":/Icons/CAD_%1.png").arg(it->second->name().c_str())), it->second->name().c_str(), this);
		action->setData( QVariant::fromValue<int>(it->first) );		


		if( it->second->canCollectPoints() )
		{
			action->setCheckable( true );
			action->setChecked( false );
		}
		tb->addAction( action  );
		ag->addAction( action );
	}

	connect(tb,         SIGNAL(actionTriggered(QAction *)), SLOT(onSelectCadTool(QAction *)) );
}

// ------------------------------------------------------------------------------------------------
void FrameworkTestApp::on_pushButtonTest1_clicked( bool checked )
{
	if( checked )
		_scene->rotateScene(10);
	else
		_scene->rotateScene(0);
	
	_scene->invalidate();
}

// ------------------------------------------------------------------------------------------------
void FrameworkTestApp::on_pushButtonTest2_clicked( bool checked )
{
	static double a = 45;
	_scene->rotateScene( a );
	_scene->invalidate();
	a+=45;
}


// ------------------------------------------------------------------------------------------------
void FrameworkTestApp::onSelectCadTool(QAction *sender)
{
	CadEngine *cad = _scene->cadEngine();

	const int toolID  = sender->data().toInt();

	// activate current selected tool
	cad->activateTool( toolID );

	_scene->invalidate();
}
